<!DOCTYPE html>
<!-- html:5 缩写-->
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    #app {
        display: block;
    }
    
    .transArea {
        background-color: rgba(123, 123, 123, 0.5);
        border: 0;
    }
    
    textarea,
    .transArea {
        /* 控制多行文本是否可调整大小 */
        resize: none;
        font-size: 2em;
        outline: none;
        box-sizing: border-box;
        width: 20vw;
        height: 20vh;
    }
    
    select {
        outline: none;
    }
    
    .translate {
        display: flex;
    }
</style>

<body>
    <!-- div#app 缩写  综合案例 Todolist-->
    <div id="app">
        <div>
            <span>翻译成的语言：</span>
            <select v-model="obj.lang">
            <option value="italy">意大利语</option>
            <option value="chinese">汉语</option>
            <option value="english">英语</option>
            <option value="japanese">日语</option>
        </select>
        </div>
        <br>
        <div class="translate">
            <textarea v-model="obj.words"></textarea>
            <div class="transArea">{{result}}</div>
        </div>
    </div>
    <script src="vue.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    <script>
        const app = new Vue({
            el: '#app',
            data: {
                str: "",
                result: "",
                obj: {
                    words: '小黑',
                    lang: 'italy'
                }
            },
            watch: {
                // watch完整写法
                obj: {
                    // 深度监视
                    deep: true,
                    //初始化时立即执行一次handler
                    immediate: true,
                    handler(newValue) {
                        //可直接挂载对象绑定属性
                        clearTimeout(this.timer)
                        this.timer = setTimeout(async() => {
                            const res = await axios({
                                url: 'https://applet-base-api-t.itheima.net/api/translate',
                                //需要传递 words 和 lang 参数
                                params: newValue
                            })
                            this.result = res.data.data
                        }, 200)
                    }
                },
                // watch简写
                result(newValue) {
                    console.log('result :>> ', this.result);
                }
            }
        })
    </script>
</body>

</html>